package com.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock锁
 */
public class Demo05 {

    public static void main(String[] args) {
        Service1 service1 = new Service1();
        MyThread myThread01 = new MyThread(service1);
        MyThread myThread02 = new MyThread(service1);
        MyThread myThread03 = new MyThread(service1);
        MyThread myThread04 = new MyThread(service1);
        MyThread myThread05 = new MyThread(service1);
        myThread01.start();
        myThread02.start();
        myThread03.start();
        myThread04.start();
        myThread05.start();
    }
}

class Service1 {
    private ReentrantLock lock = new ReentrantLock();

    public void testService() {
        lock.lock();
        for (int i = 0; i < 5; i++) {
            System.out.println("Thread.currentThread().getName()" + Thread.currentThread().getName() + ",num = " + (i + 1));
        }
        lock.unlock();
    }
}

class MyThread extends Thread {
    private Service1 service1;

    public MyThread(Service1 service1) {
        super();
        this.service1 = service1;
    }

    @Override
    public void run() {
        super.run();
        service1.testService();
    }
}